<template>
    <van-nav-bar title="充值" left-text="返回" left-arrow @click-left="onBack" />
    <van-cell-group inset>
        <van-field label="充值账户" :model-value="business.mobile" readonly />
        <van-field label="当前余额(￥)" :model-value="ismoney" readonly />
        <van-form @submit="getMoney">
            <van-field v-model="getpay" name="moneys" label="充值金额(￥)" placeholder="请输入充值金额" />
            <div style="margin: 20px auto; display: flex;justify-content: center;">
                <van-radio-group v-model="xuanzhi" direction="horizontal">
                    <van-radio :name="0">微信支付</van-radio>
                    <van-radio :name="1">支付宝支付</van-radio>
                </van-radio-group>
            </div>
            <van-button round block type="primary" native-type="submit">
                提交
            </van-button>
        </van-form>
    </van-cell-group>
</template>

<script setup>
import { getCurrentInstance, ref, reactive } from 'vue';
const { proxy } = getCurrentInstance();
const business = reactive(proxy.$cookies.get('business') ?? {});
const getpay = ref();
const xuanzhi = ref(0);
const ismoney = ref(business.money);

const getMoney = async (values)=>{
    let data={
        busid:business.id,
        money:values.moneys,
        payType:xuanzhi.value
    }
    let result = await proxy.$Api.getMoney(data);
    if(result && result.code === 1)
    {
        proxy.$showNotify({
            type:'success',
            message:result.msg,
            duration:2000
        });
        // 确保ismoney.value和result.data都为数字类型
    let currentMoney = parseFloat(ismoney.value);
    let addedMoney = parseFloat(result.data);

    // 累加后的金额
    ismoney.value = (currentMoney + addedMoney).toFixed(2); // 保留两位小数
        return;
    }else{
        proxy.$showNotify({
            type:'warning',
            message:result.msg,
            duration:2000
        });
    }
}
const onBack = () => {
    proxy.$router.back();
}
</script>